<?php
/**
 * SHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
$exc = new exchange($zys->table("attribute"), $db, 'attr_id', 'attr_name');
//分类列表
if($_REQUEST['act'] == 'list'){
	$cat_id=isset($_REQUEST['goods_type'])?$_REQUEST['goods_type']:0;//获取当前类型
	//查询当前类型名称
	$sql="select cat_name from ".ly()."goods_type where cat_id='{$cat_id}'";
	$cat_name=$db->getOne($sql);
	//将类型名称赋值到类型属性列表中
	$sql="select * from ".ly()."attribute where cat_id='{$cat_id}' order by sort_order desc";
	$data=$db->getAll($sql);
	$arr=array();
	foreach($data as $v){
		$v['cat_name']=$cat_name;
		$arr[]=$v;
	}
    $smarty->assign('goods_type',$cat_id);
	$smarty->assign('info',$arr);
	$smarty ->display("attribute.html");
}elseif($_REQUEST['act'] == 'edit'){
	$id=isset($_REQUEST['id'])?$_REQUEST['id']:0;
    if($_POST){
    	
    }else{
        $cat_id=isset($_REQUEST['goods_type'])?$_REQUEST['goods_type']:0;//获取当前类型
        $sql="select * from ".ly()."attribute where attr_id='{$id}'";
        $data=$db -> getRow($sql);
        // echo "<pre>";
        // print_r($data);
        // echo "</pre>";
        $smarty -> assign("cat_info",goods_type_select());//属性上级类型
        $smarty -> assign("show",$data);
        $smarty -> assign("goods_type",$cat_id);
        $smarty -> display("attribute_edit.html");
    }
}elseif($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update'){
	/* 检查权限 */
    admin_priv('attr_manage');

    /* 插入还是更新的标识 */
    $is_insert = $_REQUEST['act'] == 'insert';

    /* 检查名称是否重复 */
    $exclude = empty($_POST['attr_id']) ? 0 : intval($_POST['attr_id']);
    if (!$exc->is_only('attr_name', $_POST['attr_name'], $exclude, " cat_id = '$_POST[cat_id]'"))
    {
        sys_msg($_LANG['name_exist'], 1);
    }

    $cat_id = $_REQUEST['cat_id'];

    /* 取得属性信息 */
    $attr = array(
        'cat_id'          => $_POST['cat_id'],
        'attr_name'       => $_POST['attr_name'],
        'attr_index'      => $_POST['attr_index'],
        'attr_input_type' => $_POST['attr_input_type'],
        'is_linked'       => $_POST['is_linked'],
        'attr_values'     => isset($_POST['attr_values']) ? $_POST['attr_values'] : '',
        'attr_type'       => empty($_POST['attr_type']) ? '0' : intval($_POST['attr_type']),
        'attr_group'      => isset($_POST['attr_group']) ? intval($_POST['attr_group']) : 0
    );

    /* 入库、记录日志、提示信息 */
    if ($is_insert)
    {
        $db->autoExecute($zys->table('attribute'), $attr, 'INSERT');
        admin_log($_POST['attr_name'], 'add', 'attribute');
        $links = array(
            array('text' => '返回列表', 'href' => 'attribute.php?act=list&goods_type='.$_POST['cat_id']),
        );
        sys_msg("添加成功！",$links);
    }
    else
    {
        $db->autoExecute($zys->table('attribute'), $attr, 'UPDATE', "attr_id = '$_POST[attr_id]'");
        admin_log($_POST['attr_name'], 'edit', 'attribute');
        $links = array(
            array('text' => '重新编辑', 'href' => 'attribute.php?act=edit&id='.$_POST['attr_id']),
			array('text' => '返回列表', 'href' => 'attribute.php?act=list&goods_type='.$_POST['cat_id']),
        );
        sys_msg("修改成功！",$links);
    }
}elseif($_REQUEST['act'] == 'ajax_del'){
    $attr_id=isset($_REQUEST['attr_id'])?$_REQUEST['attr_id']:0;
    $sql="delete from ".ly()."attribute where attr_id='{$attr_id}'";
    $db->query($sql);
    if(@mysql_affected_rows()){
        echo "1";
    }else{
        echo "2";
    }
}elseif($_REQUEST['act'] == 'del_judge'){
    $attr_id=isset($_REQUEST['attr_id'])?$_REQUEST['attr_id']:0;
    $sql="select count(*) from ".ly()."goods_attr where attr_id='{$attr_id}'";
    $num=$db->getOne($sql);
    if($num){
         echo $num;
    }else{
         echo "0";
    }
}




function goods_type_select(){
	$sql="select cat_id,cat_name from ".ly()."goods_type";
	$data=$GLOBALS['db']->getAll($sql);
	return $data;
}
?>
